﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.ObjectModel;
using System.Linq;
using Xc.DataAccess.Core;
using Xc.DataAccess.Protocoals.WaterMeters.Models;
using Xc.DataAccess.Protocoals.WaterMeters.Models.Requests;
using Xc.DataAccess.Protocoals.WaterMeters.Services;
using System.Threading.Tasks;
using System.Net;
using Xc.DataAccess.Protocoals.WaterMeters.Extensions;

namespace Xc.WaterMeters.ServerHosts.Controllers
{
    /// <summary>
    /// 设备管理服务
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DeviceOperateController : ControllerBase, IDevice
    {
        private readonly IMeterHandleService meterHandleService;
        /// <summary>
        /// 设备管理服务
        /// </summary>
        /// <param name="meterHandleService"></param>
        public DeviceOperateController(IMeterHandleService meterHandleService)
        {
            this.meterHandleService = meterHandleService;
        }

        /// <summary>
        /// 当前在线的设备列表
        /// </summary>
        /// <param name="devcieidLike">设备编号以这个名称开始的</param>
        /// <param name="skip">忽略多少个</param>
        /// <param name="take">取多少个</param>
        /// <param name="showInvalidateDevcie">显示无效设备？</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultData<OnlineDevice>> Onlines(string devcieidLike = null, int skip = 0, int take = 1000, ShowInvalidateDevcie showInvalidateDevcie = ShowInvalidateDevcie.Hide)
        {
           return await  Task.Run(() => {
               var infos = meterHandleService.Online.Select(p=>p.Value).Where(p =>
               {
                   if (showInvalidateDevcie == ShowInvalidateDevcie.Hide)
                   {
                       return p.DeviceId != null;
                   }
                   else
                   {
                       return true;
                   }
               }).OrderByDescending(p => p.LastActiveTime).Skip(skip).Take(take).Select(p => new OnlineInfos
               {
                   DeviceId = p.DeviceId,
                   CompanyId = p.CompanyId,
                   MeterType = p.MeterType,
                   SessionID = p.SessionID,
                   LastActiveTime = p.LastActiveTime,
                   StartTime = p.LastActiveTime,
                   State = p.State,
                   DevcieIPEndpoint = p.RemoteEndPoint.ToString()
               });
               if (devcieidLike != null)
               {
                   infos = infos.Where(p => p.DeviceId.StartsWith(devcieidLike));
               }
               return ResultData<OnlineDevice>.Accept("获取成功", new OnlineDevice(infos.Count(), infos));
           });
           
        }

        /// <summary>
        /// 获取表计数据
        /// </summary>
        /// <param name="deviceId">设备编号</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> GetMeterData(string deviceId)
        {
            return await meterHandleService.GetMeterData(new GetMeterDataRequest { DevcieId = deviceId });
        }
        /// <summary>
        /// 设置表计底数
        /// </summary>
        /// <param name="setMeterValueRequest"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> SetMeterValue([FromBody] SetMeterValueRequest setMeterValueRequest)
        {
            return await meterHandleService.SetMeterValue(setMeterValueRequest);
        }
        /// <summary>
        /// 设置囤积量报警量
        /// </summary>
        /// <param name="setAlarmVolumeRequest"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> SetAlarmVolume([FromBody] SetAlarmVolumeRequest setAlarmVolumeRequest)
        {
            return await meterHandleService.SetAlarmVolume(setAlarmVolumeRequest);
        }
        /// <summary>
        /// 多次卡充值
        /// </summary>
        /// <param name="setMeterMutipleChargeRequest"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> SetMeterMutipleCharge([FromBody] SetMeterMutipleChargeRequest setMeterMutipleChargeRequest)
        {
            return await meterHandleService.SetMeterMutipleCharge(setMeterMutipleChargeRequest);
        }
        /// <summary>
        /// 获取表计最新的地址
        /// </summary>
        /// <param name="deviceId">设备编号</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> GetMeterAddress(string deviceId)
        {
            return await meterHandleService.GetMeterAddress(new GetMeterAddressRequest { DevcieId = deviceId });
        }
        /// <summary>
        /// 写开阀操作
        /// </summary>
        /// <param name="setOpenValveRequest"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> SetOpenValve([FromBody] SetOpenValveRequest setOpenValveRequest)
        {
            var temp=await meterHandleService.SetOpenValve(setOpenValveRequest);
            return temp;
        }
        /// <summary>
        /// 写关阀操作
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> SetCloseValve([FromBody] SetCloseValveRequest setCloseValveRequest)
        {
            return await meterHandleService.SetCloseValve(setCloseValveRequest);
        }
        /// <summary>
        /// 设置表具充值量
        /// </summary>
        [HttpPost]
        public async Task<Result> SetUserChange([FromBody] SetUserChangeRequest setUserChangeRequest)
        {
            return await meterHandleService.SetUserChange(setUserChangeRequest);
        }
        /// <summary>
        /// 表具恢复出厂设置
        /// </summary>
        /// <param name="deviceid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> SetRestoreFactory(string deviceid)
        {
            return await meterHandleService.SetRestoreFactory(new SetRestoreFactoryRequest { DevcieId = deviceid });
        }
        /// <summary>
        /// 设置表计服务器地址
        /// </summary>
        [HttpPost]
        public async Task<Result> SetMeterIpAddress([FromBody] SetMeterIpAddressRequest setMeterIpAddressRequest)
        {
            return await meterHandleService.SetMeterIpAddress(setMeterIpAddressRequest);
        }
        /// <summary>
        /// 修改上报周期和超时限制
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> SetMeterUploadInterval([FromBody] SetMeterUploadIntervalRequest setMeterUploadIntervalRequest)
        {
            return await meterHandleService.SetMeterUploadInterval(setMeterUploadIntervalRequest);
        }
    }
}
